package com.example.springfox.entity;

import com.example.springfox.util.EnumCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author: hett
 * @date: 2022/1/17 15:47
 */
@Schema(name = "返回值类型")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseData<T> {

    @Schema(name = "返回值的代码")
    private int code;
    @Schema(name = "返回值的信息")
    private String message;
    @Schema(name = "返回值的数据")
    private T data;

    public void parerEnum(EnumCode enumCode){
        this.code = enumCode.getCode();
        this.message = enumCode.getMessage();
    }

    public static<T> ResponseData<T> success(T data){
        ResponseData<T> responseData = new ResponseData<>();
        responseData.parerEnum(EnumCode.SUCCESS);
        responseData.setData(data);
        return  responseData;
    }

    public static<T> ResponseData<T> error(EnumCode e){
        ResponseData<T> responseData = new ResponseData<>();
        responseData.parerEnum(e);
        return  responseData;
    }

    public static<T> ResponseData<T> generator(int code, String message){
        ResponseData<T> responseData = new ResponseData<>();
        responseData.setCode(code);
        responseData.setMessage(message);
        return  responseData;
    }
}
